package windows;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.security.auth.callback.LanguageCallback;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import strategy.Crypto;

public class ABCWindow extends JFrame implements ActionListener{
	
	private char[] originalAbc;
	private char[] calculatedAbc;
	private MainWindow ref;
	private int lang;
	private JPanel northPanel;
	private JPanel southPanel;
	
	private List<JButton> origAbcButton;
	private List<JButton> calcAbcButton;
	
	public ABCWindow(char[] abc, char[] calculatedAbc, int language, MainWindow reference) {
		originalAbc = abc;
		this.calculatedAbc = calculatedAbc;
		ref = reference;
		lang = language;
		init();
	}

	private void init() {
		setLayout(new GridLayout(2, 1));
		int startX = 10;
		int startY = 10;
		int buttonHeight = 50;
		int buttonWidth = 50;
		int padding = 20;
		int autoPosition = 5;
		
		origAbcButton = new ArrayList<>();
		calcAbcButton = new ArrayList<>();
		northPanel = new JPanel();
		southPanel = new JPanel();
		
		for (int i = 0; i < originalAbc.length; i++) {
			JButton temp = new JButton(String.valueOf(originalAbc[i]));
			temp.setPreferredSize(new Dimension(buttonWidth, buttonHeight));
			northPanel.add(temp);
			origAbcButton.add(temp);
			
			
			temp = new JButton(String.valueOf(calculatedAbc[i]));
			temp.setPreferredSize(new Dimension(buttonWidth, buttonHeight));
			southPanel.add(temp);
			calcAbcButton.add(temp);
			temp.addActionListener(this);
		}
		
		setBounds(startX, startY, originalAbc.length * buttonWidth + padding + originalAbc.length * autoPosition, 2 * buttonHeight + 3 * padding);
		
		northPanel.setPreferredSize(new Dimension(originalAbc.length * buttonWidth + padding, buttonHeight + padding));
		southPanel.setPreferredSize(new Dimension(originalAbc.length * buttonWidth + padding, buttonHeight + padding));
		add(northPanel);
		add(southPanel);
		
		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		int i = 0;
		while (i < calcAbcButton.size()) {
			if (calcAbcButton.get(i) == e.getSource()) {
				break;
			}
			i++;
		}
//		System.out.println(i);
		char ret = CharInput.chooseCharacter(0);
		if (ret != ' ') {
			if (ret != '*') {
				for (int j = 0; j < calcAbcButton.size(); j++) {
					if (calcAbcButton.get(j).getText()
							.compareTo(Character.toString(ret)) == 0) {
						calcAbcButton.get(j).setText("*");
						break;
					}
				}
				calcAbcButton.get(i).setText(Character.toString(ret));
			} else {
				calcAbcButton.get(i).setText("*");
			}
			
			
		}
		System.out.println("returned character = " + ret);
		createNewCharArray();
	}
	
	private void createNewCharArray() {
		char[] calcAbc;
		if (lang == Crypto.ENG_LANGUAGE)
			calcAbc = new char[Crypto.ENG_LEGHT];
		else
			calcAbc = new char[Crypto.HUN_LEGHT];
		System.out.println(calcAbcButton.size() + " " + calcAbc.length);
		for (int i = 0; i < calcAbc.length; i++) {
			calcAbc[i] = calcAbcButton.get(i).getText().charAt(0);
		}
		ref.keywordCaesarUpdateByCalculatedAbc(calcAbc);
		this.dispose();
	}
}
